package org.doit.android.bobple.base;

import org.doit.android.bobple.R;
import org.doit.android.bobple.common.GlobalVariables;
import org.doit.android.bobple.etc.EtcActivity;
import org.doit.android.bobple.history.HistoryActivity;
import org.doit.android.bobple.recommendation.RecommendationChooseActivity;
import org.doit.android.bobple.search.SearchActivity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewStub;
import android.widget.TextView;

/**
 * @author Choi HongKi
 */
abstract public class BaseActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		super.setContentView(R.layout.common);

		TextView tvTitle = (TextView)findViewById(R.id.clh_title);
		tvTitle.setText(getTitle());

		findViewById(R.id.cl_bottom_restaurant).setOnClickListener(clickBottomMenuListener);
		findViewById(R.id.cl_bottom_hist).setOnClickListener(clickBottomMenuListener);
		findViewById(R.id.cl_bottom_etc).setOnClickListener(clickBottomMenuListener);
		findViewById(R.id.cl_bottom_recommendation).setOnClickListener(clickBottomMenuListener);

		if (GlobalVariables.getCurrentBottomMenuId() != null) {
			findViewById(GlobalVariables.getCurrentBottomMenuId()).setSelected(true);
		}

		drawOnCreate(savedInstanceState);
	}

	/**
	 * 생성 시 액티비티를 그립니다.
	 *
	 * @param savedInstanceState
	 */
	abstract protected void drawOnCreate(Bundle savedInstanceState);

	/**
	 * 공통 Content ViewStub 객체를 가져옵니다.
	 *
	 * @return 공통 Content ViewStub
	 */
	public ViewStub getContentViewStub() {
		return (ViewStub)findViewById(R.id.cl_content);
	}

	/**
	 * 공통 Content ViewStub을 전개합니다.
	 *
	 * @param layoutResource
	 *            전개할 레이아웃 리소스 아이디
	 */
	public void inflateContentViewStub(int layoutResource) {
		ViewStub contentStub = getContentViewStub();
		contentStub.setLayoutResource(layoutResource);
		contentStub.inflate();
	}

	/**
	 * 하단 메뉴 클릭 이벤트 핸들러
	 */
	private static OnClickListener clickBottomMenuListener = new OnClickListener() {
		@Override
		public void onClick(View view) {

			if (view.isSelected()) {
				return;
			}

			Intent intent = null;

			switch (view.getId()) {
				case R.id.cl_bottom_restaurant:
					intent = new Intent(view.getContext(), SearchActivity.class);
					break;
				case R.id.cl_bottom_hist:
					intent = new Intent(view.getContext(), HistoryActivity.class);
					break;
				case R.id.cl_bottom_etc:
					intent = new Intent(view.getContext(), EtcActivity.class);
					break;
				case R.id.cl_bottom_recommendation:
					intent = new Intent(view.getContext(), RecommendationChooseActivity.class);
					break;
				default:
					break;
			}

			if (intent != null) {
				GlobalVariables.setCurrentBottomMenuId(view.getId());

				view.getContext().startActivity(intent);
				/* current bottom menu is changed!! */
			}
		}
	};
}